import React, { useEffect } from 'react'
import styles from './index.module.scss'
import Tabs from '@/components/Tabs'
import { useDispatch, useSelector } from 'react-redux'
import { getUserChannels, getAllChannels } from '@/store/actions/home'
import Icon from '@/components/Icon'
import { Drawer } from 'antd-mobile'
import Channels from './components/Channels'
import { useState } from 'react'
export default function Home() {
  // 在副作用函数中 发起请求 获取频道数据
  const dispatch = useDispatch()
  useEffect(() => {
    dispatch(getUserChannels())
    dispatch(getAllChannels())
  }, [dispatch])
  // 从redux 取保存的用户频道
  const tabs = useSelector((state) => state.home.userChannels)
  // 控制频道管理抽屉的显示和隐藏
  const [drawerVisible, setDrawerVisible] = useState(false)
  // 抽屉的关闭事件
  const onClose = () => {
    setDrawerVisible(false)
  }
  return (
    <div className={styles.root}>
      {/* 最上面的频道组件 */}
      <Tabs tabs={tabs}></Tabs>
      {/* 频道 Tab 栏右侧的两个图标按钮：搜索、频道管理 */}
      <div className="tabs-opration">
        <Icon type="iconbtn_search" />
        <Icon type="iconbtn_channel" onClick={() => setDrawerVisible(true)} />
      </div>
      {/* 频道管理抽屉 */}
      <Drawer
        className="my-drawer"
        children={''}
        sidebar={drawerVisible && <Channels onClose={onClose} />}
        open={drawerVisible}
      />
    </div>
  )
}
